<?php
require_once("../../../config.inc.php");
$isReloadable = false;
require_once("../../../app/_inc/include.php");
require_once("./_inc/dataAreaFunction.php");

$errMsg = "";
if(sxREQ("reqtype") == "save"){
	$errMsg .= dataAreaMod();
}elseif(sxREQ("reqtype") == "delete"){
	$errMsg .= dataAreaDel();
}
$dataArea = sxREQ("da");

$sSQL = "SELECT * FROM data_area WHERE data_area='".$dataArea."'";
$objSDAO->query($sSQL);
if(!$row = $objSDAO->fetch()){
	die("var.'da' missing");
}
sxRDef('data_area',$row["data_area"]);
sxRDef('note',$row["note"]);
sxRDef('description',$row["description"]);

$sSQL = "SELECT * FROM data_area_doc WHERE data_area='$dataArea'";
$objSDAO->query($sSQL, "doc");

//output HTML
$aOpz = array("kMenu"=>'dam', "isReloadable"=>false);
echo(headerHTML($aOpz));
echo(bodyHTML($aOpz));
?>
<script language="JavaScript" type="text/javascript" src="../../_jsc/form.js"></script>  
<script language="JavaScript" type="text/javascript">
	function objFrmSave(){
		try{
			var objFrm = document.getElementById("objFrm");
			var objDA =  document.getElementById("data_area");
			var objMessArea =  document.getElementById("messArea");
			var errMsg = "";
			if(objDA.value == ""){
				errMsg += "<?php echo _t("missing data-area")?><br>";
				objDA.focus();
			}
			if(errMsg == ""){
				objFrm.submit();
			}else{
				objMessArea.innerHTML = errMsg;
			}
		}catch(e){
			raiseError(e.description, "save data area");
		}	
	}
</script>
  
<?php
echo getHtmlForm('objFrm');
echo getHtmlHidden('xsn',XSN);
echo getHtmlHidden('da');
echo getHtmlHidden('data_area',sxREQ('da'));
echo getHtmlHidden('reqtype','salva');

echo("<table width=\"100%\" cellpadding=\"03\" cellspacing=\"0\">\n<tr>\n<td>\n");
echo("<input type=\"hidden\" name=\"reqtype\" id=\"reqtype\" value=\"save\">");
echo("<input type=\"hidden\" name=\"xsn\" value=\"".XSN."\">");
//output comandi
$aCmd = array("insDA"=>array("pag"=>"javascript:objFrmSave()",
										"title"=>_t("Update data-area"),
										"text"=>_t("save"),
										"perm"=>"M"),
					"back"=>array("pag"=>$objUser->getHistory(0, false),
										"title"=>$objUser->getHistoryText(0),
										"text"=>$objUser->getHistoryText(0),
										"perm"=>"R")
					);
echo(commandsHTML($aCmd));	
//form data entry
echo("<table width=\"100%\" cellpadding=\"3\" cellspacing=\"2\" border='0'>\n");
echo("<tr>" .
			"<td class=\"titl01\" width=\"20%\" colspan='2'>"._t("Data-Area")."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("Name")."</td>" .
			"<td class=\"text01\">$dataArea</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("Description")."(*)</td>" .
			"<td class=\"text01\">".getHtmlText("description",80)."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("Note")."</td>" .
			"<td class=\"text01\">".getHtmlTextArea("note",80,5)."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">"._t("Document")."</td>" .
			"<td class=\"text01\">".getHtmlText("doc_name",32).
			"&nbsp;&nbsp;".getHtmlFile("document",40)."</td>" .
		"</tr>\n");
echo("</table>\n");

//documents
echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\">\n");
echo("<tr>" .
			"<td class=\"titl01\" colspan=\"2\">"._t("Documents")."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"tabh01\" width=\"90%\">"._t("File")."</td>" .
			"<td class=\"tabh01\" width=\"90%\">"._t("delete")."</td>" .
		"</tr>\n");
//output relationship: entity=parent
$numDoc = 0;
while($row = $objSDAO->fetch("doc")){
	$numDoc++;
	echo("<tr>" .
			"<td class=\"text01\"><a class=\"text01\" target=\"blank\" href=\"getDoc.php?da=$dataArea&fn=".$row["file_name"]."&xsn=".XSN."\">".sxHE($row["name"])."</a></td>" .		
			"<td class=\"text01\">" .
			"<input class=\"inpt01\" type=\"checkbox\" name=\"elim_$numDoc\" id=\"elim_$numDoc\">" .
			"<input type=\"hidden\" name=\"fn_elim_$numDoc\" id=\"fn_elim_$numDoc\" value=\"".$row["file_name"]."\">" .
			"</td>" .
		"</tr>\n");
}
if($numDoc == 0){
	echo("<tr>" .
			"<td class=\"text01\" colspan=\"2\">"._t("No documents")."</td>" .			
		"</tr>\n");
}
echo("</table>");
echo(commandsHTML($aCmd));

echo("</td>\n</tr>\n</table>\n");
echo("</form>");
echo(footerHTML());
exit();

function dataAreaMod(){
	global $objSDAO;
	$errMsg = "";
	if(trim($_REQUEST["description"]) == "")
		$errMsg .= _t("Missing description")."<br>";
	if($errMsg != ""){return $errMsg;}
	$sSQL = "UPDATE " .
			"data_area " .
			"SET description='".addslashes(sxGPC(trim($_REQUEST["description"])))."', " .
			"note='".addslashes(sxGPC(trim($_REQUEST["note"])))."'" .
			"WHERE data_area='".addslashes(sxGPC($_REQUEST["data_area"]))."'";	
	$objSDAO->exec($sSQL);
	$dataArea = addslashes(sxGPC(strtolower($_REQUEST["data_area"])));
	if(deleteDoc($dataArea, $errMsg) && insertDoc($dataArea, $errMsg)){
		$_SESSION[USEROBJ]->reLoad();
		logRecord("400", _t("Data-area")." '".$dataArea."' updated");
		//serverRedir("dataAreaView.php?da=".sxGPC($_REQUEST["data_area"])."&xsn=".XSN);
		$url = $_SESSION[USEROBJ]->getHistory(0, true);
		serverRedir($url);
		exit();
	}
	return _t($errMsg."Docs upload failed");
}
